package com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view;

import com.xiaobawang.xjsyy_idea_plugin.forest.RunPipelineRes;
import com.xiaobawang.xjsyy_idea_plugin.service.Service;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * @author synhard
 * @description 构建部署按钮点击后弹出表单
 * @date 2024/5/9 15:06
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class RunningConfigDialog extends JDialog {
    public RunningConfigDialog(RunningPanel runningPanel) {
        setModal(true);
        // 将表单面板和按钮面板添加到对话框
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(new JLabel("运行配置", SwingConstants.CENTER), BorderLayout.NORTH);

        JPanel centerPanel = new JPanel(new GridLayout(2, 1));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JPanel codeSrcPanel = new JPanel(new BorderLayout());
        codeSrcPanel.add(new JLabel("代码源", SwingConstants.CENTER), BorderLayout.NORTH);
        JPanel codeSrcNorthPanel = new JPanel(new GridLayout(1, 2));
        codeSrcNorthPanel.add(new JLabel("jsjsj/DEVC/devc-parent"));
        codeSrcNorthPanel.add(new JTextField("master"));
        codeSrcPanel.add(codeSrcNorthPanel, BorderLayout.CENTER);
        centerPanel.add(codeSrcPanel);

        JPanel variablePanel = new JPanel(new BorderLayout());
        variablePanel.add(new JLabel("变量", SwingConstants.CENTER), BorderLayout.NORTH);
        JPanel variableNorthPanel = new JPanel(new GridLayout(4, 2));
        variableNorthPanel.add(new JLabel("imageURL"));
        variableNorthPanel.add(new JLabel("ipw.clic/${lowerSysName}-docker-rel-local/devc-service"));
        variableNorthPanel.add(new JLabel("build"));
        variableNorthPanel.add(new JComboBox<>(new String[]{"是", "否"}));
        variableNorthPanel.add(new JLabel("deploy"));
        variableNorthPanel.add(new JComboBox<>(new String[]{"是", "否"}));
        variableNorthPanel.add(new JLabel("env"));
        variableNorthPanel.add(new JComboBox<>(new String[]{"dev", "test", "prod"}));
        variablePanel.add(variableNorthPanel, BorderLayout.CENTER);
        centerPanel.add(variablePanel);
        contentPane.add(centerPanel, BorderLayout.CENTER);

        JPanel southPanel = new JPanel(new GridLayout(1, 2));
        southPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        southPanel.add(new JLabel("填充上一次运行配置参数"));
        JPanel buttonGroup = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton cancel = new JButton("取消");
        cancel.addActionListener(e -> {
            // 当点击取消按钮时，关闭对话框
            JDialog dialog = (JDialog) SwingUtilities.getWindowAncestor((Component) e.getSource());
            dialog.dispose();
        });
        JButton running = new JButton("运行");
        running.addActionListener(e -> {
            // 当点击运行按钮时，调用云效接口运行流水线实例，并且关闭对话框
            RunPipelineRes runPipelineRes = Service.runPipeline(runningPanel.getDevProcess().getPipeline().getSn(),
                    runningPanel.getDevProcess().getPipeline().getReleaseWorkflowSn());
            JDialog dialog = (JDialog) SwingUtilities.getWindowAncestor((Component) e.getSource());
            dialog.dispose();
            runningPanel.getDataFetchTimer().restart();
            runningPanel.setPipelineId(runPipelineRes.getPipelineId());
            runningPanel.setPipelineRunId(runPipelineRes.getPipelineRunId());
        });
//        running.addActionListener(e -> {
//            JDialog dialog = (JDialog) SwingUtilities.getWindowAncestor((Component) e.getSource());
//            dialog.dispose();
//            runningPanel.getDataFetchTimer().restart();
//        });
        buttonGroup.add(running);
        buttonGroup.add(cancel);
        southPanel.add(buttonGroup);
        contentPane.add(southPanel, BorderLayout.SOUTH);

        // 设置对话框的默认关闭操作
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        // 设置大小和位置
        pack();
    }
}
